package com.szqhyxj.youxiangju.biz.merchantHome.shop.purchase;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.merchantHome.bean.CommoditeDetails;
import com.szqhyxj.youxiangju.biz.merchantHome.bean.DefaultSite;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.ShopModel;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.pay.AliPayActivity;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.pay.PaySuccessfulActivity;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.site.MySiteActivity;
import com.szqhyxj.youxiangju.biz.web.WebActivity;
import com.szqhyxj.youxiangju.sys.components.LoadImageUtils;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.util.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PreparePurchaseActivity extends BaseActivity implements NavigationBar.NavigationBarListener, PreparePurchaseContract.View {

    private static final String COMMODITEDETAILS = "CommoditeDetails";
    private static final String PACKAGENAME = "packageName";
    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;

    @BindView(R.id.icon)
    ImageView icon;
    @BindView(R.id.commodite_title)
    TextView commoditeTitle;
    @BindView(R.id.price)
    TextView price;

    @BindView(R.id.choose)
    CheckBox choose;
    @BindView(R.id.partnership_agreement_symbol)
    TextView partnershipAgreementSymbol;
    @BindView(R.id.type)
    TextView type;
    @BindView(R.id.quantity)
    TextView quantity;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.user_site)
    TextView userSite;
    @BindView(R.id.site)
    RelativeLayout site;
    @BindView(R.id.add_site)
    LinearLayout addSite;
    @BindView(R.id.pay)
    Button pay;
    @BindView(R.id.combined_price)
    TextView combinedPrice;
    @BindView(R.id.gifiicon)
    TextView gifiicon;
    @BindView(R.id.information_line)
    View informationLine;
    @BindView(R.id.type_name)
    LinearLayout typeName;
    @BindView(R.id.delivery_method_line)
    View deliveryMethodLine;
    @BindView(R.id.distribution)
    LinearLayout distribution;
    @BindView(R.id.distribution_line)
    View distributionLine;
    @BindView(R.id.bottom)
    LinearLayout bottom;
    @BindView(R.id.acquire)
    LinearLayout acquire;

    private PreparePurchasePresenter presenter;

    int titiletype;
    String packageid;

    public static void start(Activity activity, CommoditeDetails details) {
        Intent intent = new Intent();
        intent.setClass(activity, PreparePurchaseActivity.class);
        intent.putExtra(COMMODITEDETAILS, details);
        activity.startActivity(intent);
    }

    public static void start(Activity activity, String id) {
        Intent intent = new Intent();
        intent.setClass(activity, PreparePurchaseActivity.class);
        intent.putExtra(PACKAGENAME, id);
        activity.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prepare_purchase);
        ButterKnife.bind(this);
        CommoditeDetails details = getIntent().getParcelableExtra(COMMODITEDETAILS);
        navigationBar.setNavigationBarListener(this);
        presenter = new PreparePurchasePresenter(this);

        initEvent();
        packageid = getIntent().getStringExtra(PACKAGENAME);
        if (packageid == null || packageid.equals("")) {
            presenter.init(details);
            navigationBar.setNaviTitle("平台购机");
            gifiicon.setVisibility(View.GONE);
            titiletype = 1;
        } else {
            navigationBar.setNaviTitle("优享钜合伙人");
            gifiicon.setVisibility(View.VISIBLE);
            presenter.initData(packageid);
            presenter.setPackageId(packageid);
            titiletype = 2;
        }

        presenter.initSite();
    }

    @Override
    public void onActivityReenter(int resultCode, Intent data) {
        super.onActivityReenter(resultCode, data);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case MySiteActivity.START_100:
                if (requestCode == MySiteActivity.START_100) {
                    presenter.initSite(data.getStringExtra("addrId"));
                } else {
                    presenter.initSite();
                }
                break;
            case AliPayActivity.CODE_2001:

                PaySuccessfulActivity.start(PreparePurchaseActivity.this, "", titiletype);
                break;
            case AliPayActivity.CODE_2002:
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public void initEvent() {
        choose.setChecked(true);
        presenter.onChecked(true);
        pay.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
        choose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                presenter.onChecked(b);
                if (b) {
                    pay.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
                } else {
                    pay.setBackgroundColor(getResources().getColor(R.color.colorGray));
                }
            }
        });
        presenter.setByType(ShopModel.OrderEntity.platform);
        /*way.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.platform:
                        presenter.setByType(ShopModel.OrderEntity.platform);
                        break;
                    case R.id.superior:
                        presenter.setByType(ShopModel.OrderEntity.superior);
                        break;
                }
            }
        });*/
    }

    @Override
    public void navigationLeft() {
        finish();
    }

    @Override
    public void navigationRight() {
    }

    @Override
    public void showCommodite(CommoditeDetails data) {
        LoadImageUtils.loadImage(icon, data.getSmallPic());
        commoditeTitle.setText(data.getPackageName());
        type.setText(data.getBrandName());
        quantity.setText("/" + data.getDeviceNum() + "台");
        price.setText(StringUtils.initPrice(data.getPackagePrice()));
        combinedPrice.setText(StringUtils.initPrice(data.getPackagePrice()));
    }

    @Override
    public void showSite(DefaultSite defaultSite) {
        addSite.setVisibility(View.GONE);
        site.setVisibility(View.VISIBLE);
        name.setText(defaultSite.getUsername());
        userSite.setText(defaultSite.getAddress());
        phone.setText(defaultSite.getPhone());
    }

    @Override
    public void showNventory(String nventorySum) {
    /*    if (Integer.parseInt(nventorySum) < 5) {
            superior.setEnabled(false);
            superior.setBackground(getResources().getDrawable(R.drawable.background__gray04));
            superior.setTextColor(getResources().getColor(R.color.colorGray));

        }*/
    }

    @Override
    public void navMySite() {
        MySiteActivity.start(this);
    }

    @Override
    public void navPay(String orderStr) {
        AliPayActivity.start(this, orderStr, 1);
    }

    @OnClick({R.id.add_site, R.id.site, R.id.choose, R.id.pay, R.id.partnership_agreement_symbol})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.add_site:
                navMySite();
                break;
            case R.id.site:
                navMySite();
                break;
            case R.id.pay:
                presenter.save();
                break;
            case R.id.partnership_agreement_symbol:
                String path = String.format("partnership");
                WebActivity.startToH5(this, "合伙人合作协议", path);
                break;
        }
    }
}
